package com.example.mymarket.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.mymarket.Data_model.Channel;
import com.example.mymarket.R;

import java.util.List;


//适配器
public class ChannerlAdapter extends BaseAdapter {
    private Context ctx;
    private List<Channel> channels;

    public ChannerlAdapter(Context ctx, List<Channel> channels) {
        this.ctx = ctx;
        this.channels = channels;
    }

    @Override
    public int getCount() {
        return this.channels.size();
    }

    @Override
    public Object getItem(int i) {
        return this.channels.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        //定义一个类装文本和图片
        ViewHolder viewHolder;

        if (view == null){
            view = LayoutInflater.from(this.ctx).inflate(R.layout.home_channel_item,null);
            viewHolder = new ViewHolder();
            viewHolder.home_channel_img = view.findViewById(R.id.home_channel_img);
            viewHolder.home_channel_txt = view.findViewById(R.id.home_channel_txt);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.home_channel_txt.setText(this.channels.get(i).getName());
        Glide.with(view.getContext())
                .load(this.channels.get(i).getIconUrl())
                .into(viewHolder.home_channel_img);

        return view;
    }

    static class ViewHolder{
        private ImageView home_channel_img;
        private TextView home_channel_txt;
    }
}
